<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<script type="text/javascript" src="<?php echo base_url() . 'js/tiny_mce/tiny_mce.js' ?>"></script>
<script type="text/javascript">
	tinyMCE.init({
	        mode : "textareas",
	        theme : "advanced",
			skin : "o2k7",
	        editor_selector : "mceSimpleEditor",
	        editor_deselector : "mceSimpleNoEditor",
	        relative_urls : false,
			
			plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

	        // Theme options
	        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
	        theme_advanced_toolbar_location : "top",
	        theme_advanced_toolbar_align : "left",
	        theme_advanced_statusbar_location : "bottom",
	        theme_advanced_resizing : true,
	
	        // Skin options
	        skin : "o2k7",
	        skin_variant : "silver",
	
	        // Example content CSS (should be your site CSS)
	        content_css : "css/example.css",
	
	        // Drop lists for link/image/media/template dialogs
	        template_external_list_url : "js/template_list.js",
	        external_link_list_url : "js/link_list.js",
	        external_image_list_url : "js/image_list.js",
	        media_external_list_url : "js/media_list.js",

			content_css : "<?php echo base_url(); ?>skins/<?php echo $template_f; ?>/tinymce.css"		 	
	});
</script>
<div class="middle myclear">
	
	<h3 class="padtop10 padbottom10">
		Gửi tin nhắn hệ thống
	</h3>
	
	<form id="frmSend" name="frmSend" action="<?php echo site_url('message/send'); ?>" method="post">
		<table cellpadding="0" cellspacing="0" border="0">
			<tr>
				<td class="pad5 ver-middle">
					Loại:
				</td>
				<td class="pad5 ver-middle">
					<select name="slcType" id="slcType">
						<option value="-2">---------------</option>
						<option value="-1">Tất cả user</option>
						<option value="1">Admin của agency</option>
					</select>
				</td>
				<td class="pad5 ver-middle">
					Nhóm:
				</td>
				<td class="pad5 ver-middle">
					<select name="slcGroup" id="slcGroup">
						<option value="-2">------------------------</option>
						<option value="-1">Tất cả các nhóm</option>
						<option value="0">End User</option>
						<?php
						foreach($lstGroup as $group)
						{
						?>
							<option value="<?php echo $group['groupid']; ?>"><?php echo $group['groupname']; ?></option>
						<?php
						}
						?>
					</select>
				</td>
				
			</tr>
		</table>
		<table cellpadding="0" cellspacing="0" border="0" class="martop10">
			<tr>
				<td class="pad5">
					Tùy chọn:
				</td>
				<td class="pad5 ver-middle">
					<input type="checkbox" name="chkMail" id="chkMail" checked="checked" /> Gửi mail
				</td>
				<td class="pad5 ver-middle">
	 				<input type="checkbox" name="chkMess" id="chkMess" checked="checked" /> Gửi tin nhắn
				</td>
				<td class="pad5 ver-middle">
	 				<input type="checkbox" name="chkEditor" id="chkEditor" checked="checked" onclick="switchEditor(this);"/> Sử dụng Text Editor
				</td>
				<td class="pad5 ver-middle">
				 		<input type="checkbox" name="chkUser" id="chkUser" /> Gửi chính xác user
				</td>
			</tr>
		</table>
		
		<table cellpadding="0" cellspacing="0" border="0" class="martop10">
			<tr>
				<td class="pad5 ver-middle">
					Tìm kiếm: 
				</td>
				<td class="pad5 ver-middle">
					<input type="text" id="txtMssSearch" name="txtMssSearch" style="padding: 4px 5px;" size="40" />
				</td>
				<td class="pad5 ver-middle">
					<select name="slcSearchType" id="slcSearchType">
						<option value="1">Theo Username</option>
						<option value="2">Theo Tên</option>
					</select>
				</td>
				<td class="pad5 ver-middle">
					<input type="button" value="Search" onclick="mssSearchUser('1');" />
				</td>
				
				<td class="pad5 ver-middle">
					<img alt="Loading..." src="/images/ajaxLoading.gif" id="loading" style="display: none;" />
				</td>
				
			</tr>
		</table>
		
		<div id="divSlcUser" class="padtop20 padbottom20">
			<input type="hidden" id="hddSlcUser" name="hddSlcUser" />
			<h3>
				Danh sách User đã chọn
			</h3>
			<div id="lstSelectUsesr"></div>
		</div>
		
		<div id="lstUser"></div>
		
		
		<div class="padtop10 padbottom10">
			<h3>
				Tiêu đề
			</h3>
			
			<input type="text" name="txtTitle" id="txtTitle" style="padding: 4px 5px;" size="100" />
			<br/>
			<h3>
				Nội dung
			</h3>
			<br/>
			<div id="divEditor">
				<textarea name="txtContent" id="txtContent" class="mceSimpleEditor pad5" rows="50" cols="110"></textarea>
			</div>
			<textarea name="txtContent2" id="txtContent2" class="pad5" rows="50" cols="110" style="display: none;"></textarea>
			
			<br /><br />
			<input type="button" value="Send" onclick="mssSend();" />
		</div>
	</form>
</div>
<script type="text/javascript">
	$(document).ready(function() {
		$('#txtUsername').focus(function (){
			$(this).val('');
		}).focusout(function () {
			var val = $.trim($(this).val());
			if (val == '') {
				$(this).val('Nhập username');
			}
		});
	});
	
	function selectUser(uId)
	{
		var currSlcId = $.trim($('#hddSlcUser').val());
		var tmp = currSlcId.split(',');
		var chk = true;
		var num = tmp.length;
		for(var i=0; i<num;i++)
		{
			if($.trim(tmp[i]) != '' && $.trim(tmp[i]) == uId)
			{
				chk = false;
				break;
			}
		}
		if(chk)
		{
			var uname = $.trim($('#uname_' + uId).html());
			var fname = $.trim($('#fname_' + uId).html());
			var str = '<p class="padtop10" id="slc_' + uId + '">';
			str += '<img alt="Remove" src="/admin/images/x.gif" class="ver-middle marright5 cursor-pointer" onclick="removeUser(\'' + uId + '\');" />';
			str += '<span class="ver-middle">' + uname +  ' - ' + fname + '</span>';
			str += '</p>';
			$('#tr_' + uId).remove();
			$('#lstSelectUsesr').append(str);
			currSlcId += (currSlcId == '') ? uId : ',' + uId;
			$('#hddSlcUser').val(currSlcId);
		}
	}
	
	function removeUser(uId)
	{
		var currSlcId = $.trim($('#hddSlcUser').val());
		var arrId = currSlcId.split(',');
		var str = '';
		jQuery.each(arrId, function(){
			if($.trim(this) != '' && $.trim(this) != uId)
			{
				str += (str =='') ? uId : ',' + uId;
			}
		});
		$('#slc_' + uId).remove();
		$('#hddSlcUser').val(str);
	}
	
	function mssSearchUser(cPage)
	{
		var type = $.trim($('#slcType').val());
		var group = $.trim($('#slcGroup').val());
		var stype = $.trim($('#slcSearchType').val());
		var keyword = $.trim($('#txtMssSearch').val());
		if(keyword.length < 3)
		{
			alert('Bạn phải nhập tối thiểu 3 ký tự!');
			return;
		}
		var onSuccess = function(data)
		{
			$('#loading').hide();
			if($.trim(data) == 'login')
			{
				window.location.reload(true);
			}
			else if($.trim(data) == 'notright')
			{
				alert('Bạn không có quyền thực hiện chức năng này!');
			}
			else
			{
				$('#lstUser').html(data);
			}
		};
		
		var onError = function()
		{
			$('#loading').hide();	
		};
		
		$('#loading').show();
		getAjax('/admin/message/search', 'type=' + type + '&g=' + group + '&stype=' + stype + '&cpage=' + cPage + '&keyword=' + UrlEncode.encode(keyword), '', 'GET', '', false, onSuccess, onError);
	}
	
	
	function tinyMCEGetValue(eID)
	{
		return tinyMCE.get(eID).getContent();
	}
	
	function tinyMCEGetCheckValue(eID)
	{
		var a = tinyMCE.get(eID).getContent();
		a = stripHtmlTags(a);
		a = a.replace(/&nbsp;/gi, '');
		return a;
	}
	
	function tinyMCESetValue(eID, content)
	{
		tinyMCE.get(eID).setContent(content);
	}
	
	function switchEditor(obj)
	{
		if($(obj).attr('checked'))
		{
			$('#txtContent2').hide();
			$('#divEditor').show();
		}
		else
		{
			$('#txtContent2').show();
			$('#divEditor').hide();
		}
	}
	
	function mssSend()
	{
		var title = $.trim($('#txtTitle').val());
		if(title == '')
		{
			alert('Bạn hãy nhập tiêu đề.');
			return;
		}
		if($('#chkEditor').attr('checked'))
		{
			var content = tinyMCEGetCheckValue('txtContent');
			if(content == '')
			{
				alert('Bạn hãy nhập nội dung!');
				return;
			}
		}
		else
		{
			var content = $.trim($('#txtContent2').val());
			if(content == '')
			{
				alert('Bạn hãy nhập nội dung!');
				return;
			}
		}
		
		if($('#chkUser').attr('checked'))
		{
			if($.trim($('#hddSlcUser').val()) == '' )
			{
				alert('Bạn chưa chọn user!');
				return;
			}
		}
		else
		{
			var type = $.trim($('#slcType').val());
			var group = $.trim($('#slcGroup').val());
			
			if(type == '-2' || group == '-2')
			{
				alert('Bạn hãy lựa chọn kiểu hoặc nhóm user');
				$('#slcType').focus();
				return;
			}
		}
		
		
		
		if(confirm('Bạn có chắc chắn muốn gửi?'))
		{
			$('#frmSend').submit();
		}
	}
	
	
	
</script>